/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class RemoveBusContent extends Content {

	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	
	private ServiceBus sc = null;
	
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			
			HashMap p = new HashMap();
			
			listener.onContentChanged(DomainContentConstants.REMOVE_BUS,
					DomainContentConstants.DOMAIN_DETAIL, p);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}

	};
	
	public RemoveBusContent(DomainMessages cons,BifangGWTServiceAsync sservice, ServiceBus ssc){
		this.cons = cons;
		this.service = sservice;
		this.sc = ssc;
		
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(3);
		panel.setWidth("80%");
		HTML h = new HTML(cons.busBasic());
		panel.add(h);
		
		Grid table = new Grid(3, 2);
		table.setWidth("100%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);
		
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		
		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.code());
		table.setHTML(2, 0, cons.desc());
		
		table.setHTML(0, 1, sc.getName());
		table.setHTML(1,1,sc.getCode());
		table.setHTML(2,1,sc.getDescription());
		panel.add(table);
		
		HTML hh = new HTML(cons.confirmRemove());
		panel.add(hh);
		HorizontalPanel h1 = new HorizontalPanel();
		Button b1 = new Button(cons.remove());
		
		b1.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				
				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.domain.RemoveBusAction");
				req.setRequest(sc.getId());
				service.call(req, cb);
			}
		});
		
		Button cancel = new Button(cons.cancel());
		cancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				HashMap p = new HashMap();
				
				listener.onContentChanged(DomainContentConstants.REMOVE_BUS,
						DomainContentConstants.BUS_DETAIL, p);
				
			}
		});
		
		h1.add(b1);
		h1.add(cancel);
		panel.add(h1);
		
		initWidget(panel);
		
		
	}
	
}
